﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_RetirementDtls : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IRetirementDetailBLL dtlsBLL = new RetirementDetailBLL();
        private IRetirementBLL retireBLL = new RetirementBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {           

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblRetireDtlsSeq.Text = DataBinder.Eval(DataItem, "RETIRE_DTLS_SEQ").ToString();
                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FULL_NAME").ToString(), DataBinder.Eval(DataItem, "EMP_SEQ").ToString()));
                    cmbEmployee.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EMP_SEQ"));
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                }
                else
                {

                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        public string GetBgYear()
        {
            DataSet ds = retireBLL.LoadRetirements(Convert.ToInt32(Request.QueryString["Sequence"].ToString()), (int?)null);
            DataRowCollection drc;
            string BgYear = "0";

            if (ds.Tables.Count > 0)
            {
                drc = ds.Tables[0].Rows;
            }
            return BgYear;// drc[0]["RETIRE_YEAR"].ToString();
        }
        //protected void FillDataToEmployees()
        //{
        //    IList<RetirementInfo> rList = retireBLL.ListRetirementsBySequence(Convert.ToInt32(Request.QueryString["Sequence"].ToString()));

        //    IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes("1", "", rList[0].BGYear.ToString());


        //    foreach (EmployeeInfo List in infoList)
        //    {
        //        RadComboBoxItem item = new RadComboBoxItem();

        //        item.Text = List.Person.FullName;
        //        item.Value = List.Sequence.ToString();

        //        item.Attributes.Add("EMP_NAME", List.Person.FullName);
        //        item.Attributes.Add("BIRTH_DATE", string.Format("{0:dd/MM/yyyy}", List.Person.BirthDate.ToString()));
        //        item.Attributes.Add("POS_NAME", List.Position == null ? "" : List.Position.Name);
        //        item.Attributes.Add("CLASS_NAME", List.Class.Name);
        //        item.Attributes.Add("JOB_SITE_NAME", List.Position == null ? "" : List.Position.JobSite == null ? "" : List.Position.JobSite.Name);

        //        cmbEmp.Items.Add(item);
        //        item.DataBind();
        //    }
            
        //}
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            dtlsBLL.Insert(Convert.ToInt32(Request.QueryString["Sequence"].ToString())
                , cmbEmployee.SelectedValue
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            dtlsBLL.Update(Convert.ToInt32(lblRetireDtlsSeq.Text)
                , Convert.ToInt32(Request.QueryString["Sequence"].ToString())
                , cmbEmployee.SelectedValue
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        


        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();
            int RetireYear = Convert.ToInt32(Session["RETIREYEAR"].ToString());

            DataSet ds = empBLL.LoadEmployeeRetire(Session["EMPTYPE"].ToString(), RetireYear, RetireYear, true, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = string.Format("{0:dd/MM/yyyy}", dt.Rows[i]["BIRTH_DATE"]);
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["CITIZEN"].ToString();
                tdJobName.Width = Unit.Pixel(50);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }



     }

    
}